<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">公司管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick"
            >基地管理</span
          >
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex">
          <el-button class="btn-light-blue" @click="handleAdd()"
            >添加基地信息</el-button
          >
        </div>
        <!-- <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
        >
          <el-form-item label="">
            <el-cascader
              v-model="searchForm.companyId"
              placeholder="请选择公司名称"
              :options="companyArray"
              :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }"
              @change="changeTreeItem"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button class="btn-light-blue" @click="onSubmit">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form> -->
      </div>
      <NxTable
        :table-cols="tableCols"
        :table-data="tableData"
        :pag-ination="pagination"
        class="table-wrap"
        @refresh="refresh"
      >
        <template slot="operation" slot-scope="scope">
          <span @click="handleEdit(scope.row)" class="nx-btn nx-edit"
            >编辑</span
          >
          <span @click="handleDelete(scope.row)" class="nx-btn nx-del"
            >删除</span
          >
        </template>
      </NxTable>
    </div>
    <AddEditDialog
      :addEditId="addEditId"
      :addEditStatus="addEditStatus"
      :addEditIsDialog="addEditIsDialog"
    >
    </AddEditDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'
import { Page } from '@/utils/pagination'

import {
  getSysCompanyBaseList,
  delSysCompanyBase,
  getCompanyInfoTree,
} from '@/api/company'

export default {
  components: {
    NxTable,
    AddEditDialog,
  },
  // 表格数据
  data() {
    return {
      pagination: new Page(),

      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        id: this.$route.query.id,
      },
      // 表格字段
      tableCols: [
        {
          label: '公司编码',
          prop: 'code',
          align: 'center',
        },
        {
          label: '基地名称',
          prop: 'name',
          align: 'center',
        },
        // {
        //   label: "公司名称",
        //   prop: "companyName",
        //   align: "center",
        // },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 180,
        },
      ],
      tableData: [],
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      companyArray: [], // 公司集合
    }
  },
  // 初始化加载
  mounted() {
    this.getList()

    // 获取公司信息树形列表
    this.getCompanyInfoTree()
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.searchForm.pageNum = 1
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        companyId: '',
      }

      this.onSubmit()
    },
    // 查询列表
    getList() {
      getSysCompanyBaseList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.pagination.count = res.data.total
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delSysCompanyBase({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg,
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
      this.typeDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
      this.addEditIsDialog = false
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }

      // 查询
      this.getList()
    },
    // 获取公司列表
    getCompanyInfoTree() {
      getCompanyInfoTree({}).then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data
        }
      })
    },
    // 职位
    handlePosition(row) {
      this.$router.push({
        path: `/system/company/departPosition`,
        query: {
          departId: row.id,
        },
      })
    },
  },
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
